package com.mytest.provider.es.serviceImpl;

import com.mytest.provider.es.clients.userFen;
import com.mytest.provider.es.mapper.ElasticRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class indexservice {
    @Autowired
    private userFen userFen;
    @Autowired
    private ElasticRepository elasticRepository;

    /*@Autowired
    GoodsRepoistory goodsRepoistory;*/


    /*public ESUser buildservice(Spubo spubo){
        ESUser esuser = new ESUser();
//        将spubo里与goods相同的属性拷贝到goods中去
        BeanUtils.copyProperties(spubo,esuser);

//        all    所有需要被搜索的信息，包含标题，分类，甚至品牌
        //查询到分类的names集合
        List<String> names = categoryClient.queryNameByIds(Arrays.asList(spubo.getCid1(), spubo.getCid2(), spubo.getCid3()));
        String all = spubo.getTitle()+" "+StringUtils.join(names," ");
        esuser.setAll(all);


//        skus
        List<Sku> skus = goodsClient.querySkuBySpuId(spubo.getId());
//        由于只要sku的id,title,image,price

        List<Long> prices = new ArrayList<>();
        List<Map<String,Object>> skumaplist = new ArrayList<>();
        skus.forEach(sku -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id",sku.getId());
            map.put("title",sku.getTitle());
            map.put("price",sku.getPrice());
//            判断图片是否为空,是"  ",不是就放图片的第一个
            map.put("image",StringUtils.isBlank(sku.getImages())?"":sku.getImages().split(",")[0]);
//            存入skumap集合中
            skumaplist.add(map);
            //    将price存入集合
            prices.add(sku.getPrice());
        });
        esuser.setSkus(JsonUtils.serialize(skumaplist));


//        price
        esuser.setPrice(prices);


//        specs  可搜索的规格参数，key是参数名，值是参数值
//        将可以搜索的规格参数取出来
        List<SpecParam> specParams = specClient.querySpecParam(null, spubo.getCid3(), true,null);
//        判断是公共的,还是特有的,各自取出
//         取出spudetail
        SpuDetail spuDetail = goodsClient.querySpuDetailById(spubo.getId());

//         由于spudetail中的gengeric_spec是json字符串,要转为map
//        Json:对象转json,json转对象
//        通用
        Map<Long,Object> gengericmap = JsonUtils.nativeRead(spuDetail.getGenericSpec(), new TypeReference<Map<Long, Object>>() {
        });
//        特有
        Map<Long,List<String>> specialMap = JsonUtils.nativeRead(spuDetail.getSpecialSpec(), new TypeReference<Map<Long, List<String>>>() {
        });

        Map<String, Object> spec =new HashMap<>();   //e.g   规格参数名 : 值(在spudetail中)

        specParams.forEach(specParam -> {
            Long id = specParam.getId();         //规格参数的id
            String name = specParam.getName();   //拿到规格参数名

            Object o =null;
            if (specParam.getGeneric()){         //判断规格参数是公共还是特有
                 o = gengericmap.get(id);        //公共id所对应的值
//                判断是否有单位
                if (specParam.getNumeric()&&o!=null){
//                数值类型有可能需要加分段
                    o = this.chooseSegment(o.toString(),specParam);
                }
            }else {
                o = specialMap.get(id);
            }
            if (null==o){
                o="其他";
            }
            spec.put(name,o);
        });
        esuser.setSpecs(spec);

        return esuser;
    }*/

  /*  private String chooseSegment(String value, SpecParam p) {
        double val = NumberUtils.toDouble(value);                       //将键对应的值改为double
        String result = "其它";
        // 保存数值段
        for (String segment : p.getSegments().split(",")) {       //拿到每一个区间字符串
            String[] segs = segment.split("-");                   //拿到区间字符串的最大,最小的数组
            // 获取数值范围
            double begin = NumberUtils.toDouble(segs[0]);                //拿到区间最小值,并将字符串改为double类型
            double end = Double.MAX_VALUE;                               //拿到区间最大值,并将字符串改为double类型
            if(segs.length == 2){
                end = NumberUtils.toDouble(segs[1]);
            }
            // 判断是否在范围内
            if(val >= begin && val < end){
                if(segs.length == 1){
                    result = segs[0] + p.getUnit() + "以上";
                }else if(begin == 0){
                    result = segs[1] + p.getUnit() + "以下";
                }else{
                    result = segment + p.getUnit();                         //添加单位,获取数据库里的单位
                }
                break;
            }
        }
        return result;
    }*/

  /*  *//**
     * 从生产者拿到spuid,-->spu-->spubo-->goods
     * 给索引库新增或修改
     * @param id
     *//*
    public void createIndex(Long id) {
        Spu spu = this.goodsClient.querySpuById(id);
        Spubo spubo = new Spubo();
        BeanUtils.copyProperties(spu,spubo);
        Goods goods = this.buildservice(spubo);
        this.goodsRepoistory.save(goods);
    }*/

    /**
     * 将索引库里的spuid的数据删除
     * @param id
     */
//    public void deleteIndex(Long id) {
//        this.goodsRepoistory.deleteById(id);
//    }
}
